package org.vectrics.recruiter.struts.tag;

import java.util.Iterator;

import org.vectrics.SystemException;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.resume.email.EmailedResumeAttachment;
import org.vectrics.recruiter.user.resume.email.EmailedResumeInfo;
import org.vectrics.struts.VectricsTagSupport;

public class EmailedResumeBodyTag extends VectricsTagSupport {
	public static final long serialVersionUID = 62678345;
	private String emailedResumeId = null;
	private String readOnly = "false";
	
	public EmailedResumeBodyTag() {
		super();
	}

	protected String makeStartTag() {
		if (emailedResumeId == null)
			throw new SystemException("'emailedResumeId' not set for tag");
		EmailedResumeInfo emailResumeInfo = RecruitServiceLocator.getRecruitUserService().findEmailedResume(new Long(this.emailedResumeId));
		if (emailResumeInfo == null)
			throw new SystemException("Object not found for id: " + this.emailedResumeId);

		
		Table attachmentTable = new Table();
		Iterator iter = emailResumeInfo.getAttachments().iterator();
		while (iter.hasNext()) {
			EmailedResumeAttachment attachment = (EmailedResumeAttachment)iter.next();
			if 	((attachment.getDocument().getOriginalFileName() == null) 
					|| (attachment.getDocument().getOriginalFileName().trim().length() == 0)) {
				Row row = attachmentTable.addRow();
				row.setDefaultCellFontClass("stdFont");
				row.addCell().addText(attachment.getDocument().getOriginalFileName());
				String contentHtml = TextHelper.getHtmlParagraphs("subFont", attachment.getDocument().getTextContent());
				Cell cell = row.addCell();
				cell.addElement(contentHtml);
				cell.addElement("<br>");
				if (this.readOnly.equals("false")) {
					cell.addElement(new EmailedResumeAttachmentCategoryCombo(this.getLocale(), attachment));
				}
			}
		}
		return attachmentTable.toString();
	}

	/**
	 * @return Returns the emailedResumeId.
	 */
	public String getEmailedResumeId() {
		return emailedResumeId;
	}

	/**
	 * @param emailedResumeId The emailedResumeId to set.
	 */
	public void setEmailedResumeId(String emailedResumeId) {
		this.emailedResumeId = emailedResumeId;
	}

	/**
	 * @return Returns the readOnly.
	 */
	public String getReadOnly() {
		return readOnly;
	}

	/**
	 * @param readOnly The readOnly to set.
	 */
	public void setReadOnly(String readOnly) {
		this.readOnly = readOnly;
	}

}
